package com.kudian.restful.service;

import com.kudian.common.persistence.HibernateDao;
import com.kudian.common.service.BaseService;
import com.kudian.common.utils.StringUtils;
import com.kudian.restful.dao.AdDao;
import com.kudian.restful.dao.LayoutDao;
import com.kudian.restful.entity.Ad;
import com.kudian.restful.entity.Layout;
import com.kudian.restful.vo.ad.AdItemVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/18.
 */
@Service
@Transactional(readOnly = true)
public class AdService extends BaseService<Ad, Integer> {
    private static final Logger logger = Logger.getLogger(AdService.class);

    @Autowired
    private AdDao adDao;

    @Autowired
    private LayoutDao layoutDao;

    @Autowired
    private MediaResService mediaResService;

    @Override
    public HibernateDao<Ad, Integer> getEntityDao() {
        return adDao;
    }

    /**
     * 取得轮播广告
     */
    public List<AdItemVO> carousel() {
        // direction 1:上方 2:中部
        // 轮播取上方的广告
        List<AdItemVO> ret = null;
        List<Ad> ads = adDao.findbyorder("sortOrder", true, Restrictions.eq("direction", (short) 1), Restrictions.eq("enabled", (short) 1));
        for (Ad ad : ads) {
            if (ret == null)
                ret = new ArrayList<AdItemVO>();

            AdItemVO advo = new AdItemVO();
            advo.setType(ad.getAdType());
            advo.setSortOrder(ad.getSortOrder());
            if (StringUtils.isNotBlank(ad.getMediaId())) {
                advo.setUrl(mediaResService.getResUrl(ad.getMediaId()));
            } else {
                advo.setUrl("");
            }

            ret.add(advo);
        }
        return ret;
    }

    /**
     *
     * @param vo
     * @return
     */
    public MidAdRetVO queryMidAd(MidAdVO vo) {
        MidAdRetVO retVO = new MidAdRetVO();
        // position 1: 上 2:中
        List<Layout> lys = layoutDao.find(Restrictions.eq("position", (short) 1), Restrictions.eq("layoutGroup", vo.getLayoutGroup()));
        retVO.setTitles(queryByLayout(lys));

        // position 1: 上 2:中
        lys = layoutDao.find(Restrictions.eq("position", (short) 2), Restrictions.eq("layoutGroup", vo.getLayoutGroup()));
        retVO.setItems(queryByLayout(lys));
        return retVO;
    }

    private List<AdItemVO> queryByLayout(List<Layout> lys) {
        List<AdItemVO> ret = new ArrayList<AdItemVO>();
        if (lys == null || lys.isEmpty()) {
            return ret;
        }

        List<Short> adids = new ArrayList<Short>();
        /** 取数组id */
        for (Layout ly : lys) {
            adids.add(ly.getAdId());
        }
        List<Ad> ads = adDao.findbyorder("sortOrder", true, Restrictions.in("adId", adids.toArray()));

        for (Ad ad : ads) {
            if (ret == null)
                ret = new ArrayList<AdItemVO>();

            AdItemVO vo = new AdItemVO();
            vo.setType(ad.getAdType());
            vo.setSortOrder(ad.getSortOrder());
            vo.setUrl(mediaResService.getResUrl(ad.getMediaId()));
            vo.setGoodsId(ad.getGoodsId());
            ret.add(vo);
        }
        return ret;
    }
}
